package com.example.demo.practice.enterprise;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.enterprisemanager.involveinfo;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class EnterpriseAccess extends Activity {
    private ServerHttp serverHttp;
    private AutoCompleteTextView autoCompleteTextView;
    private ListView listView;
    private List<involveinfo> mlistInfo = new ArrayList();
    private ArrayList<ArrayList<String>> item = new ArrayList();
    private TitleView title;
    private ArrayList<String> allcpnName = new ArrayList<>();
    private ImageView ima4;

    public class ListViewAdapter extends BaseAdapter {
        View[] itemViews;

        public ListViewAdapter(List<involveinfo> mlistInfo) {
            // TODO Auto-generated constructor stub
            itemViews = new View[mlistInfo.size()];
            for(int i=0;i<mlistInfo.size();i++){
                involveinfo getInfo=mlistInfo.get(i);    //获取第i个对象
                //调用makeItemView，实例化一个Item
                itemViews[i]=makeItemView(
                        getInfo.getText1(), getInfo.getText2(),getInfo.getText3(), getInfo.getText4()
                );
            }
        }

        public int getCount() {
            return itemViews.length;
        }

        public View getItem(int position) {
            return itemViews[position];
        }

        public long getItemId(int position) {
            return position;
        }

        //绘制Item的函数
        private View makeItemView(String strText1, String strText2,String strText3,String strText4) {
            LayoutInflater inflater = (LayoutInflater) EnterpriseAccess.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // 使用View的对象itemView与R.layout.item关联
            View itemView = inflater.inflate(R.layout.access_item, null);

            // 通过findViewById()方法实例R.layout.item内各组件
            TextView text1 = itemView.findViewById(R.id.text1);
            text1.setText(strText1);    //填入相应的值
            TextView text2 = itemView.findViewById(R.id.text2);
            text2.setText(strText2);
            TextView text3 = itemView.findViewById(R.id.text3);
            text3.setText(strText3);
            TextView text4 = itemView.findViewById(R.id.text4);
            text4.setText(strText4);

            return itemView;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            involveinfo getObject = mlistInfo.get(position);
            View view = View.inflate(EnterpriseAccess.this,
                    R.layout.access_item, null);

            ImageView ima1 = view.findViewById(R.id.t1);
            ImageView ima2 = view.findViewById(R.id.t2);
            ImageView ima3 = view.findViewById(R.id.t3);


            ima1.setOnClickListener(new ImageView.OnClickListener(){
                @Override
                public void onClick(View v){
                    ima4.setVisibility(View.VISIBLE);
                    ima4.setImageResource(R.drawable.t1);
                }
            });

            ima2.setOnClickListener(new ImageView.OnClickListener(){
                @Override
                public void onClick(View v){
                    ima4.setVisibility(View.VISIBLE);
                    ima4.setImageResource(R.drawable.t2);
                }
            });

            ima3.setOnClickListener(new ImageView.OnClickListener(){
                @Override
                public void onClick(View v){
                    ima4.setVisibility(View.VISIBLE);
                    ima4.setImageResource(R.drawable.t3);
                }
            });

            return view;
        }
    }

    public void setInfo(){
        mlistInfo.clear();
        int i=0;
        while(i<item.size()){
            involveinfo information = new involveinfo();
            information.setId(Integer.toString(i));
            information.setText1(item.get(i).get(0));
            information.setText2(item.get(i).get(1));
            information.setText3(item.get(i).get(2));
            information.setText4(item.get(i).get(3));

            mlistInfo.add(information); //将新的info对象加入到信息列表中
            i++;
        }
    }

    public void setserachInfo(String name){
        mlistInfo.clear();
        int i=0;
        while(i<item.size()){
            if(item.get(0).equals(name)){
                involveinfo information = new involveinfo();
                information.setId(Integer.toString(i));
                information.setText1(item.get(i).get(0));
                information.setText2(item.get(i).get(1));
                information.setText3(item.get(i).get(2));
                information.setText4(item.get(i).get(3));

                mlistInfo.add(information); //将新的info对象加入到信息列表中

                break;
            }
            i++;
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray=serverHttp.getData();
            List<List<String>> list=new LinkedList<List<String>>();
            for(int i=0;i<jsonArray.length();i++){
                ArrayList<String> l=new ArrayList<>();
                try {
                    JSONObject json= (JSONObject) jsonArray.get(i);
                    l.add(json.getString("name"));
                    l.add(json.getString("adress"));
                    l.add(json.getString("controller"));
                    l.add(json.getString("contactnumber"));
                    item.add(l);
                    allcpnName.add(json.getString("name"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            setInfo();  //给信息赋值函数，用来测试
            listView.setAdapter(new  ListViewAdapter(mlistInfo));

            serverHttp.clear();
            serverHttp=null;
            ImageView button= (ImageView) findViewById(R.id.searchBtn);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String nameS = autoCompleteTextView.getText().toString().trim();
                    setserachInfo(nameS);  //给信息赋值函数，用来测试
                    listView.setAdapter(new  ListViewAdapter(mlistInfo));
                }
            });
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enterprise_access);
        ima4 = findViewById(R.id.t4);
        title = (TitleView)findViewById(R.id.title_text_tv);
        title.setText("企业准入");
        autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        listView = (ListView)findViewById(R.id.listView);

        serverHttp=new ServerHttp(handler);
        serverHttp.setUrl("enterpriseAccess");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,allcpnName);
        autoCompleteTextView.setAdapter(adapter);

        ima4.setOnClickListener(new ImageView.OnClickListener(){
            @Override
            public void onClick(View v){
                ima4.setVisibility(View.GONE);
            }
        });
    }
}
